<?php

namespace addons\cms\controller\api;

use addons\cms\library\Theme;
use addons\cms\model\Block;
use addons\cms\model\Channel;
use app\common\model\cms\Prefix;
use fast\Http;
use think\Config;
use think\exception\HttpResponseException;
use think\Hook;
use think\captcha\Captcha;
use think\Log;

/**
 * 公共
 */
class Common extends Base
{
    protected $noNeedLogin = '*';

    protected $noNeedRight = '*';
    /**
     * 初始化
     */
    public function init()
    {
        //焦点图
        $bannerList = [];
        $list = Block::getBlockList(['name' => 'uniappfocus', 'row' => 5]);
        foreach ($list as $index => $item) {
            $bannerList[] = ['image' => cdnurl($item['image'], true), 'url' => $item['url'], 'title' => $item['title']];
        }

        //配置信息
        $upload = Config::get('upload');

        //如果非服务端中转模式需要修改为中转
        if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
            //临时修改上传模式为服务端中转
            set_addon_config($upload['storage'], ["uploadmode" => "server"], false);

            $upload = \app\common\model\Config::upload();
            // 上传信息配置后
            Hook::listen("upload_config_init", $upload);

            $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
        }

        $upload['cdnurl'] = $upload['cdnurl'] ?: cdnurl('', true);
        //上传地址强制切换为使用本地上传，云存储插件会自动处理
        $upload['uploadurl'] = url('/api/common/upload', '', false, true);

        //支付列表和默认支付方式
        $paytypearr = array_filter(explode(',', Config::get('cms.paytypelist')));
        $defaultPaytype = Config::get('cms.defaultpaytype');
        $defaultPaytype = in_array($defaultPaytype, $paytypearr) ? $defaultPaytype : reset($paytypearr);

        //登录类型列表
        $logintypearr = array_filter(explode(',', Config::get('cms.logintypelist')));
        $config = [
            'upload'         => $upload,
            //登录类型列表
            'logintypearr'   => $logintypearr,
            'paytypelist'    => implode(',', $paytypearr),
            'defaultpaytype' => $defaultPaytype,
            'sitename'       => Config::get('cms.sitename'),
            'sitelogo'       => cdnurl(Config::get('cms.sitelogo'), true),
        ];

        $data = array_merge($config, [
            'bannerList' => $bannerList,
            '__token__'  => $this->request->token()
        ]);

        //合并主题样式，判断是否预览模式
        $isPreview = stripos($this->request->SERVER("HTTP_REFERER"), "mode=preview") !== false;
        $themeConfig = $isPreview && \think\Session::get("previewtheme") ? \think\Session::get("previewtheme") : Theme::get();
        $themeConfig = Theme::render($themeConfig);
        $data = array_merge($data, $themeConfig);

        $this->success('', $data);
    }

    public function level_info()
    {
        $level_id = $this->request->param('level_id');
        $level = \app\common\model\user\Level::where('id', $level_id)->field('level,createtime,updatetime',true)->find();
        if($level['background_image']){
            $level['background_image'] = cdnurl($level['background_image'], true);
        }
        $lang = getHeaderLang();
        if ($lang != getDefaultLang()) {
            $level['content'] = $level['content_'.$lang];
            $level['name'] = $level['name_'.$lang];
        }
        $this->success('等级描述', $level);
    }

    public function level_list()
    {
        $level_id = $this->request->param('level_id');
        $level = \app\common\model\user\Level::order('level asc')->field('level,createtime,updatetime',true)->select();

        $lang = getHeaderLang();
        if($level){
            foreach ($level as $key => $value){
                if ($lang != getDefaultLang()) {
                    $value->name = $value['name_'.$lang];
                    $value->content = $value['content_'.$lang];
                }
                if ($value['background_image']){
                    $value->background_image = cdnurl($value['background_image'], true);
                }
            }
        }
        $this->success('等级列表', $level);
    }

    public function index_config(){
        $banner_list = Block::where('name', 'uniappfocus')->order('weigh desc,id desc')->field('id,type,title,image')->select();
        $nav1 = \app\common\model\user\Level::order('level asc')->field('id,level,name,name_zh_cn,name_en,image')->select();
        $nav2 = get_addon_config('cms')['flagtype'] ?? [];
        $lang = getHeaderLang();
        if ($lang != getDefaultLang()) {
            languageTranslation($nav2,$lang);
        }
        $new1 = [];
        foreach ($nav1 as $index => $item) {
            if ($lang != getDefaultLang()) {
                $item->name = $item['name_'.$lang];
            }
        }
//        $new1 = array_merge($nav1, [['id' => 0,'image' => 'https://wanzaiapp.oss-cn-chengdu.aliyuncs.com/uploads/20241221/a68a7704c7e8527a1041ad942d738884.png']]);
        $registered_info = \addons\cms\model\Page::where('type', 'registered')->field('content,content_zh_cn,content_en')->find();
        $privacy_info = \addons\cms\model\Page::where('type', 'privacy')->field('content,content_zh_cn,content_en')->find();
        $user_info_ = \addons\cms\model\Page::where('type', 'user_info')->field('content,content_zh_cn,content_en')->find();
        $notice_info = \addons\cms\model\Page::where('type', 'notice')->field('content,content_zh_cn,content_en')->find();

        if ($lang != getDefaultLang()) {
            $registered = $registered_info['content_'.$lang];
            $privacy = $privacy_info['content_'.$lang];
            $user_info = $user_info_['content_'.$lang];
            $notice = $notice_info['content_'.$lang];
        }else{
            $registered = $registered_info['content'];
            $privacy = $privacy_info['content'];
            $user_info = $user_info_['content'];
            $notice = $notice_info['content'];
        }
        $tel = get_addon_config('cms')['tel'];
        $email = get_addon_config('cms')['email'];
        $this->success('', compact('banner_list', 'nav1', 'nav2', 'registered', 'privacy', 'user_info', 'notice','tel','email'));
    }

    public function get_lang()
    {
        $list = get_addon_config('cms');
        $this->success('',$list['lang_list']);
    }

    public function get_mobile_prefix()
    {
        $field = 'id,name,num';
        $lang = getHeaderLang();
        if ($lang != getDefaultLang()){
            $field = 'id,name_'.$lang.' as name,num';
        }
        $list = Prefix::field($field)->order('weigh desc,id desc')->select();
        $this->success('',$list);
    }

    public function get_channel_list()
    {
        $list = get_addon_config('cms')['flagtype'] ?? [];
        $lang = getHeaderLang();

        if ($lang != getDefaultLang()){
            languageTranslation($list,$lang);
        }
        $this->success('',$list);
    }
    /**
     * 获取分类
     */
    public function getCategory()
    {
        $model_id = $this->request->param('model');
        $parent_id = $this->request->param('channel');
        $menu_index = (int)$this->request->param('menu_index/d', 0);
//        对于首页，应用初始化，app原生导航,无法给传参，默认传-1，取导航的设置参数
        if ($model_id == -1) {
            $model_id = Theme::getFirstParam('model', $menu_index);
        }
        if ($parent_id == -1) {
            $parent_id = Theme::getFirstParam('channel', $menu_index);
        }
        $tabList = [];
        $channelList = Channel::where('status', 'normal')
            ->where('model_id', $model_id)
            ->where('parent_id',0)
//            ->where("FIND_IN_SET('recommend', flag)")
//            ->where('type', 'in', ['list'])
//            ->where(function ($query) use ($model_id, $parent_id) {
//                if (!empty($model_id)) {
//                    $query->where('model_id', $model_id);
//                }
//                if (!empty($parent_id)) {
//                    $query->where('parent_id', $parent_id);
//                }
//            })
            ->field('id,parent_id,model_id,name,name_en,name_zh_cn')
            ->order('weigh desc,id desc')
            ->select();

        $lang = getHeaderLang();
        foreach ($channelList as $index => $item) {
            if ($lang != getDefaultLang()) {
                $item->name = $item['name_'.$lang];
            }

            $children = Channel::where('status', 'normal')
                ->where('model_id', $model_id)
                ->where("parent_id",$item['id'])
                ->field('id,parent_id,model_id,name,name_en,name_zh_cn')
                ->order('weigh desc,id desc')
                ->select();
            foreach ($children as  $citem) {
                if ($lang != getDefaultLang()) {
                    $citem->name = $citem['name_'.$lang];
                }
            }
            $data = [
                'id' => $item['id'],
                'title' => $item['name'],
                'children' => $children
            ];
            $tabList[] = $data;
        }
//        $list = get_addon_config('cms')['flagtype'] ?? [];
//        $lang = getHeaderLang();
//
//        if ($lang != getDefaultLang()){
//            languageTranslation($list,$lang);
//        }
        $this->success('', $tabList);
    }

    /**
     * 获取关联数据
     */
    public function selectpage()
    {
        $dispatch = $this->request->dispatch();
        $method = ['\think\addons\Route', 'execute'];
        $vars = array_merge($this->request->param(), $dispatch['var'], ['controller' => 'ajax', 'action' => 'selectpage']);
        return \think\App::invokeMethod($method, $vars);
    }

    /**
     * 获取验证码
     */
    public function captcha()
    {
        $ident = $this->request->param('ident');
        $captcha = new Captcha((array)Config::get('captcha'));
        $res = $captcha->entry($ident)->getContent();
        $this->success('获取成功', 'data:image/png;base64,' . base64_encode($res));
    }


    /**
     * 获取小程序码
     */
    public function getWxCode()
    {
        $id = $this->request->post('id');
        $model = $this->request->post('model');
        $version = $this->request->post('version', 'release');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $user_id = $this->auth->isLogin() ? $this->auth->id : '';
        $resource = '';
        $fileStream = (new \addons\booking\library\Wechat\Service)->getWxCodeUnlimited([
            'scene'       => "invite_id={$user_id}&id={$id}",
            'env_version' => $version, //要打开的小程序版本。正式版为 release，体验版为 trial，开发版为 develop
            'page'        => $model ? 'pages/article/detail' : 'pages/product/detail'
        ]);
        if (is_null(json_decode($fileStream))) {
            try {
                $img = imagecreatefromstring($fileStream);
                ob_start();
                imagepng($img);
                $resource = ob_get_clean();
            } catch (\Exception $e) {
                Log::write($e->getMessage());
                $this->error("Failed to get WeChat QR code！");
            }
        } else {
            $config = get_addon_config('cms');
            if ($config['wxapp']) {
                $localFile = ROOT_PATH . 'public' . $config['wxapp'];
                if (is_file($localFile)) {
                    $resource = file_get_contents($localFile);
                } else {
                    $resource = Http::get(cdnurl($config['wxapp'], true));
                }
            }
            if (config('app_debug')) {
                Log::write($fileStream);
            }
        }
        if (!$resource) {
            Log::write($fileStream);
            $this->error("Failed to get the QR code！");
        }
        $base64_data = base64_encode($resource);
        $base64_file = 'data:image/png;base64,' . $base64_data;
        $this->success('获取成功', $base64_file);
    }
}
